package com.hr.checkrepetition.controller;

import com.hr.checkrepetition.callback.MyCallBack;
import com.hr.checkrepetition.config.DelayedQueueConfig;
import com.hr.checkrepetition.config.RabbitMQConfig;
import com.hr.checkrepetition.model.vo.ArticleCheckVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.Set;

/**
 * @program: check-repetition
 * @description:
 * @author: hjk
 * @create: 2021-07-08 17:47
 **/
@Slf4j
@RequestMapping("ttl")
@RestController
public class SendMsgController {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private MyCallBack myCallBack;
    @Autowired
    private StringRedisTemplate redisTemplate;


    @PostConstruct
    public void init(){
        rabbitTemplate.setConfirmCallback(myCallBack);
        /**
         * true：
         * 交换机无法将消息进行路由时，会将该消息返回给生产者
         * false：
         * 如果发现消息无法进行路由，则直接丢弃
         */
        rabbitTemplate.setMandatory(true);
        //设置回退消息交给谁处理
        rabbitTemplate.setReturnsCallback(myCallBack);
    }

    /**
     * 发送延时消息
     * @param message
     */
    @GetMapping("sendMsg/{message}")
    public void sendMsg(@PathVariable String message){
        log.info("当前时间：{},发送一条信息给两个 TTL 队列:{}", new Date(), message);
        rabbitTemplate.convertAndSend(RabbitMQConfig.X_EXCHANGE, "XA", "消息来自 ttl 为 10S 的队列: "+message);
        rabbitTemplate.convertAndSend(RabbitMQConfig.X_EXCHANGE, "XB", "消息来自 ttl 为 40S 的队列: "+message);
    }

    /**
     * 根据 传递的延时时间 定时发送消息
     * @param message
     * @param ttlTime
     */
    @GetMapping("sendExpirationMsg/{message}/{ttlTime}")
    public void sendMsg(@PathVariable String message,@PathVariable String ttlTime) {
        rabbitTemplate.convertAndSend("X", "XC", message, correlationData ->{
            correlationData.getMessageProperties().setExpiration(ttlTime);
            return correlationData;
        });
        log.info("当前时间：{},发送一条时长{}毫秒 TTL 信息给队列 C:{}", new Date(),ttlTime, message);
    }

    /**
     * 基于延时插件 实现延时消息
     * @param message
     * @param delayTime
     */
    @GetMapping("sendDelayMsg/{message}/{delayTime}")
    public void sendMsg(@PathVariable String message,@PathVariable Integer delayTime) {
        rabbitTemplate.convertAndSend(DelayedQueueConfig.DELAYED_EXCHANGE_NAME, DelayedQueueConfig.DELAYED_ROUTING_KEY, message,
                correlationData ->{
                    correlationData.getMessageProperties().setDelay(delayTime);
                    return correlationData;
                });
        log.info(" 当 前 时 间 ： {}, 发送一条延迟 {} 毫秒的信息给队列 delayed.queue:{}", new Date(),delayTime, message);
    }

    @GetMapping("sendMessage/{message}")
    public void sendMessage(@PathVariable String message){
        //指定消息 id 为 1
        CorrelationData correlationData1=new CorrelationData("1");
        String routingKey="confirm_routing_key";
        ArticleCheckVo articleCheckVo = ArticleCheckVo.builder().checkTitle("测试一下").checkWordNum(10).checkTime(new Date()).reportNo("666666").build();
        rabbitTemplate.convertAndSend(DelayedQueueConfig.CONFIRM_EXCHANGE_NAME,routingKey,articleCheckVo,correlationData1);
        //CorrelationData correlationData2=new CorrelationData("2");
        //routingKey="key2";

        //rabbitTemplate.convertAndSend(DelayedQueueConfig.CONFIRM_EXCHANGE_NAME,routingKey,message+routingKey,correlationData2);
        log.info("发送消息内容:{}",message);
    }

    @RequestMapping("/test")
    public String test(@RequestParam("current") Integer current, @RequestParam("size") Integer size){
        redisTemplate.opsForZSet().add("linkId:2", 1 +"", 11);
        redisTemplate.opsForZSet().add("linkId:2", 2 +"", 22);
        redisTemplate.opsForZSet().add("linkId:2", 3 +"", 33);
        redisTemplate.opsForZSet().add("linkId:2", 4 +"", 44);
        redisTemplate.opsForZSet().add("linkId:2", 5 +"", 55);
        redisTemplate.opsForZSet().add("linkId:2", 6 +"", 66);
        redisTemplate.opsForZSet().incrementScore("linkId:2", 7 +"", 1);
        redisTemplate.opsForZSet().incrementScore("linkId:2", 6 +"", 1);

        final Set<String> range = redisTemplate.opsForZSet().range("linkId:2", current, size);
        System.err.println(range);
        return "success";
    }
}
